You will find in this book code examples about >10 Roslyn Source Code Generator (RSCG ) that can be usefull for you. That means, you will write more elegant and concise code - even if the generators code is not always nice to look.
I have done due diligence to test the RSCG that I have show to you here. However, I cannot guarantee that will fit your code . That means that you can test it for your case and, because all are open source on Github.com , you can contribute to improve them ;-)
For each chapter, you will find
Please send me an email to ignatandrei@yahoo.com
In the introduction I have put the links to get you started with RSCG .
And, if you bought this book from Amazon , you are entitled to have 1 hour free of consultancy with me . I can help you make one.
A Roslyn Source Code Generator (RSCG ) is a program that generates code in the compile time, based on the previous source code and/or another data . This new source code is added to the compilation and compile with the previous source code.
For creating the RSCG you will simply create a .NET Standard 2.0 project,add those 2 references
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />and start implementing
public interface ISourceGenerator
{
void Initialize(GeneratorInitializationContext context);
void Execute(GeneratorExecutionContext context);
}Start from examples at https://github.com/dotnet/roslyn-sdk/tree/main/samples/CSharp/SourceGenerators Also, you can read the source code for the RSCG presented in this book.
Start read
https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.md
and
https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md .
After that , you can play with the examples from https://github.com/dotnet/roslyn-sdk/tree/main/samples/CSharp/SourceGenerators or from https://sourcegen.dev/ ( see AutoNotify in the dropdown )
Glad that you asked. You can see in action a RSCG for automatically generating code for automating testing ( see DynamicMocking ) , parsing enum ( see Enum ) , generating controllers actions from a interface ( SkinnyControllers ), currying functions and many more. In this book you will find more than 10 examples of some RSCG that can help you. Also, you can find the source code of the examples at https://github.com/ignatandrei/RSCG_Examples.
Nuget : https://www.nuget.org/packages/ThisAssembly
link : https://www.clarius.org/ThisAssembly/
author :Daniel Cazzulino
The ThisAssembly.Info allows you access to the Assembly Information as constants, instead of going to reflection each time. I found useful to see the assembly version right away in any project that I have.
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/ApplicationVersion
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/
link : http://msprogrammer.serviciipeweb.ro/category/roslyn/
author :Andrei Ignat
This will generate code to fast parsing a int or a string to an enum
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/Enum
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/JsonByExampleGenerator/
link : https://github.com/hermanussen/JsonByExampleGenerator/
author :Robin Hermanussen
This will generate C# classes from json files.
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/JsonToClass
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/
link : http://msprogrammer.serviciipeweb.ro/category/roslyn/
author :Andrei Ignat
This will generate code for a POCO to generate copy constructor and deconstructor
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/CopyConstructor
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/GeneratedMapper/
link : https://github.com/ThomasBleijendaal/GeneratedMapper
author :Thomas Bleijendaal
AutoMapping from a POCO to a DTO. Lots of customizations
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/DTOMapper
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/SkinnyControllersCommon/ https://www.nuget.org/packages/SkinnyControllersGenerator/
link : http://msprogrammer.serviciipeweb.ro/category/roslyn/
author :Andrei Ignat
This will generate code for WebAPI for each method of a field in the controller
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/SkinnyControllers
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/DasMulli.DataBuilderGenerator/
link : https://github.com/dasMulli/data-builder-generator
author :Martin Andreas Ulrich
Implements the Builder Design pattern for any class. Useful , at least, for test projects
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/DP_Builder
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/
link : http://msprogrammer.serviciipeweb.ro/category/roslyn/
author :Andrei Ignat
This will generate code to retrieve the values of properties directly, not by reflection
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/MetadataFromObject
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/MockSourceGenerator/
link : https://github.com/hermanussen/MockSourceGenerator/
author :Robin Hermanussen
This will generate Mock classes directly for any interface - with your implementation.
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/DynamicMocking
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/
link : http://msprogrammer.serviciipeweb.ro/category/roslyn/
author :Andrei Ignat
This will generate code to decorate methods with anything you want ( stopwatch, logging , authorization…)
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/MethodDecorator
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/PartiallyApplied/
link : https://github.com/JasonBock/PartiallyApplied
author :Andrei Ignat
This will generate curry for your functions
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/PartiallyFunction
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/
link : http://msprogrammer.serviciipeweb.ro/category/roslyn/
author :Andrei Ignat
This will generate code to add IFormattable to any class, based on the properties of the class
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/IFormattable
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/BeaKona.AutoInterfaceGenerator
link : https://github.com/beakona/AutoInterface
author :beakona
Implement the Design Pattern Decorator. Based on template - you can modify the source code generated
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/DP_Decorator
All Generators: https://github.com/ignatandrei/RSCG_Examples/
Nuget : https://www.nuget.org/packages/AOPMethodsCommon/ https://www.nuget.org/packages/AOPMethodsGenerator/
link : http://msprogrammer.serviciipeweb.ro/category/roslyn/
author :Andrei Ignat
This will generate code to add function to be used with Entity Framework to search for any property of a class
The code that you will use is
The code that is generated is
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/PropertyExpressionGenerator
All Generators: https://github.com/ignatandrei/RSCG_Examples/
There are more RSCG that you could see - here is a list that you may want to look at: